package loader

import (
	"log"
	"os"

	"github.com/spf13/viper"
	"xincan.com.cn/tarot-go/internal/common/constants"
)

// localApplicationConfigLoader
// Title 				localApplicationConfigLoader
// Description 			全局参数配置: 加载初始化原始配置信息
// Author  				alittlexincan@163.com 					2024/07/22 11:36
// Update  				alittlexincan@163.com 					2024/07/22 11:36
type localApplicationConfigLoader struct {

	// viper  Viper实例
	viper *viper.Viper

	// name  配置文件名称
	name string

	// suffix 配置文件后缀
	suffix string

	// path 配置文件路径
	path string
}

// LocalApplicationConfigLoaderBuilder
// Title 				LocalApplicationConfigLoaderBuilder
// Description 			本地参数配置构建器
// Author  				alittlexincan@163.com 					2024/07/22 11:36
// Update  				alittlexincan@163.com 					2024/07/22 11:36
type LocalApplicationConfigLoaderBuilder struct {

	// localViper 本地配置实例
	*localApplicationConfigLoader
}

// NewLocalApplicationConfigLoaderBuilder
// Title 				NewLocalApplicationConfigLoaderBuilder
// Description 			创建本地参数配置构建器
// Author  				alittlexincan@163.com 					2024/07/22 11:36
// Update  				alittlexincan@163.com 					2024/07/22 11:36
// Return    			*LocalApplicationConfigBuilder     		本地参数配置构建器实例
func NewLocalApplicationConfigLoaderBuilder() *LocalApplicationConfigLoaderBuilder {
	return &LocalApplicationConfigLoaderBuilder{
		localApplicationConfigLoader: &localApplicationConfigLoader{
			viper:  viper.New(),
			name:   constants.DefaultConfigName,
			suffix: constants.DefaultConfigSuffix,
			path:   constants.DefaultConfigFile,
		},
	}
}

// WithName
// Title 				WithName
// Description 			构造器: 设置配置文件名称
// Author      			alittlexincan@163.com       							2024/07/22 11:36
// Update    			alittlexincan@163.com       							2024/07/22 11:36
// Param    			name 							string					配置文件名称
// Return    			*LocalApplicationConfigLoaderBuilder     				本地参数配置构建器实例
func (builder *LocalApplicationConfigLoaderBuilder) WithName(name string) *LocalApplicationConfigLoaderBuilder {
	builder.name = name
	return builder
}

// WithSuffix
// Title 				WithSuffix
// Description 			构造器: 设置配置文件后缀
// Author      			alittlexincan@163.com       							2024/07/22 11:36
// Update    			alittlexincan@163.com       							2024/07/22 11:36
// Param    			suffix 							string					配置文件后缀
// Return    			*LocalApplicationConfigLoaderBuilder     				本地参数配置构建器实例
func (builder *LocalApplicationConfigLoaderBuilder) WithSuffix(suffix string) *LocalApplicationConfigLoaderBuilder {
	builder.suffix = suffix
	return builder
}

// WithPath
// Title 				WithPath
// Description 			构造器: 配置文件路径
// Author      			alittlexincan@163.com       							2024/07/22 11:36
// Update    			alittlexincan@163.com       							2024/07/22 11:36
// Param    			path 							string					配置文件路径
// Return    			*LocalApplicationConfigLoaderBuilder     				本地参数配置构建器实例
func (builder *LocalApplicationConfigLoaderBuilder) WithPath(path string) *LocalApplicationConfigLoaderBuilder {
	builder.path = path
	return builder
}

// Build
// Title 				Build
// Description 			构造器: 构建本地参数配置实例
// Author      			alittlexincan@163.com       							2024/07/22 11:36
// Update    			alittlexincan@163.com       							2024/07/22 11:36
// Return    			*LocalApplicationConfig     							本地参数配置实例
func (builder *LocalApplicationConfigLoaderBuilder) Build() (*viper.Viper, string) {
	return builder.ConfigLoad()
}

// ConfigLoad
// Title 				ConfigLoad
// Description 			加载本地配置信息
// Author      			alittlexincan@163.com       							2024/07/22 11:36
// Update    			alittlexincan@163.com       							2024/07/22 11:36
// Return    			*viper.Viper     										Viper实例
func (local *localApplicationConfigLoader) ConfigLoad() (*viper.Viper, string) {
	// 1. 创建新的Viper实例（避免使用全局实例）
	local.viper.SetConfigType(local.suffix)
	local.viper.SetConfigFile(local.path)
	if err := local.viper.ReadInConfig(); err != nil {
		log.Fatalf("Viper初始化本地配置信息失败, 文件地址: %s, 失败信息: %v", local.path, err)
	}

	//2.  获取当前激活环境, 尝试加载已经激活的环境配置
	active := local.viper.GetString("application.active")
	if active != "" {
		// 尝试加载开发环境配置
		activeConfigFile := constants.GetDefaultActiveConfigFile(active)
		local.viper.SetConfigFile(activeConfigFile)
		err := local.viper.MergeInConfig()
		if err == nil {
			local.path = local.path + ", " + activeConfigFile
		} else {
			// 根据实际情况决定环境配置文件是否为必需
			if os.IsNotExist(err) {
				log.Printf("加载[%s]环境配置: %s不存在，跳过加载\n", active, activeConfigFile)
			} else {
				log.Fatalf("合并[%s]环境配置: %s失败, %v", active, activeConfigFile, err)
			}
		}
	}
	return local.viper, local.path
}
